package ua.races.dom.docBuilder;

import org.jdom.Element;
import ua.races.vo.Client;
import java.util.List;
import java.util.ListIterator;

public class ClientDocumentBuilder extends DocumentBuilder {

    protected List<Client> clients;

    public ClientDocumentBuilder(List<Client> clients) {
        this.clients = clients;
    }

    public void build() {
        Client client = null;

        Element root = new Element("clients");

        ListIterator<Client> iterator = clients.listIterator();

        while (iterator.hasNext()) {
            Element clientElement = new Element("client");
            client = iterator.next();

            Element clientId = new Element("ClientID");
            clientId.setText(String.valueOf(client.getClientId()));
            clientElement.addContent(clientId);

            Element firstName = new Element("FirstName");
            firstName.setText(client.getFirstName());
            clientElement.addContent(firstName);

            Element lastName = new Element("LastName");
            lastName.setText(client.getLastName());
            clientElement.addContent(lastName);

            Element phoneNumber = new Element("PhoneNumber");
            if (client.getPhoneNumber()!=null) {
            phoneNumber.setText(client.getPhoneNumber());
            } else {
                phoneNumber.setText("");
            }
            clientElement.addContent(phoneNumber);

            Element email = new Element("email");
            email.setText(client.getEmail());
            clientElement.addContent(email);

            root.addContent(clientElement);
        }

        document.addContent(root);

    }
}
